package com.ckk.eurekaclient.service.mq;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ckk.eurekaclient.constants.MqQueue;
import com.ckk.eurekaclient.enums.SyncTypeEnum;
import com.ckk.eurekaclient.mapper.MarkEditionMapper;
import com.ckk.eurekaclient.mapper.MarkMapper;
import com.ckk.eurekaclient.pojo.MarkEditionPo;
import com.ckk.eurekaclient.pojo.MarkPo;
import com.ckk.eurekaclient.pojo.MarkerPo;
import com.ckk.eurekaclient.util.JsonUtil;
import com.ckk.eurekaclient.vo.mq.MarkMqInsertVo;
import com.ckk.eurekaclient.vo.mq.SyncDataReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * MqListener
 *
 * @author momo
 */

@Slf4j
@Component
public class MqListener {

    @Autowired
    private MarkMapper markMapper;

    @Autowired
    private MarkEditionMapper markEditionMapper;

    /**
     * 标记点 后 -> 前
     * @param msg
     */
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = MqQueue.EXCHANGE_BEHIND_FRONT, durable = "true", autoDelete = "false"), exchange = @Exchange(value = MqQueue.EXCHANGE_BEHIND_FRONT, type = "topic", durable = "true"), key = "#")})
    @Transactional(rollbackFor = Exception.class)
    public void listenStackInfoSync(Message msg) {
        log.info("接收到来自后台的数据:[{}]", msg);
        byte[] body = msg.getBody();
        SyncDataReqVO dataVO = JsonUtil.byte2json(body, SyncDataReqVO.class);
        if (SyncTypeEnum.INSERT.getCode().equals(dataVO.getType())) {
            MarkMqInsertVo vo = JsonUtil.json2Obj(dataVO.getData(), MarkMqInsertVo.class);
            if (vo != null) {
                log.info("开始填充=====>guid:[{}]", vo.getMarkPo().getGuid());
//                markMapper.insert(vo.getMarkPo());
//                markEditionMapper.insert(vo.getMarkEditionPo());
                log.info("填充成功");
                return;
            }
        } else if (SyncTypeEnum.UPDATE.getCode().equals(dataVO.getType())) {
            MarkMqInsertVo vo = JsonUtil.json2Obj(dataVO.getData(), MarkMqInsertVo.class);
            if (vo != null) {
                log.info("开始修改=====>guid:[{}]", vo.getMarkPo().getGuid());
//                markMapper.updateById(vo.getMarkPo());
//                markEditionMapper.insert(vo.getMarkEditionPo());
                log.info("修改成功");
                return;
            }
        } else if (SyncTypeEnum.DELETE.getCode().equals(dataVO.getType())) {
            String vo = JsonUtil.json2Obj(dataVO.getData(), String.class);
            if (vo != null) {
                log.info("开始删除=====>guid:[{}]", vo);
//                markMapper.delete(new LambdaQueryWrapper<MarkPo>().eq(MarkPo::getGuid, vo));
//                markEditionMapper.delete(new LambdaQueryWrapper<MarkEditionPo>()
//                        .eq(MarkEditionPo::getMarkGuid, vo));
                log.info("删除成功");
                return;
            }
        } else {
            return;
        }
    }

}
